home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
icon
/
packages.lha
/
packages
/
atari
/
ats.arc
/
TESTS.ARC
/
RSG.ICN
< prev
next >
Wrap
Text File
|
1990-03-28
|
6KB
|
278 lines
global defs, ifile, in, limit, prompt, tswitch
record nonterm(name)
record charset(chars)
record query(name)
procedure main(args)
local line, plist, s, opts
# procedures to try on input lines
plist := [define,generate,grammar,source,comment,prompter,error]
defs := table() # table of definitions
defs["lb"] := [["<"]] # built-in definitions
defs["rb"] := [[">"]]
defs["vb"] := [["|"]]
defs["nl"] := [["\n"]]
defs[""] := [[""]]
defs["&lcase"] := [[charset(&lcase)]]
defs["&ucase"] := [[charset(&ucase)]]
defs["&digit"] := [[charset(&digits)]]
opts := getopt(args,"tl+l+")[1]
limit := \opts["l"] | 1000
tswitch := \opts["t"]
&random := \opts["s"]
ifile := [&input] # stack of input files
prompt := ""
while in := pop(ifile) do { # process all files
repeat {
if *prompt ~= 0 then writes(prompt)
line := read(in) | break
while line[-1] == "\\" do line := line[1:-1] || read(in) | break
(!plist)(line)
}
close(in)
}
end
# process alternatives
#
procedure alts(defn)
local alist
alist := []
defn ? while put(alist,syms(tab(upto('|') | 0))) do move(1) | break
return alist
end
# look for comment
#
procedure comment(line)
if line[1] == "#" then return
end
# look for definition
#
procedure define(line)
return line ?
defs[(="<",tab(find(">::=")))] := (move(4),alts(tab(0)))
end
# define nonterminal
#
procedure defnon(sym)
local chars, name
if sym ? {
="'" &
chars := cset(tab(-1)) &
="'"
}
then return charset(chars)
else if sym ? {
="?" &
name := tab(0)
}
then return query(name)
else return nonterm(sym)
end
# note erroneous input line
#
procedure error(line)
write("*** erroneous line: ",line)
return
end
# generate sentences
#
procedure gener(goal)
local pending, symbol
pending := [nonterm(goal)]
while symbol := get(pending) do {
if \tswitch then
write(&errout,symimage(symbol),listimage(pending))
case type(symbol) of {
"string": writes(symbol)
"charset": writes(?symbol.chars)
"query": {
writes(&errout,"*** supply string for ",symbol.name," ")
writes(read()) | {
write(&errout,"*** no value for query to ",symbol.name)
break
}
}
"nonterm": {
pending := ?\defs[symbol.name] ||| pending | {
write(&errout,"*** undefined nonterminal: <",symbol.name,">")
break
}
if *pending > \limit then {
write(&errout,"*** excessive symbols remaining")
break
}
}
}
}
write()
end
# look for generation specification
#
procedure generate(line)
local goal, count
if line ? {
="<" &
goal := tab(upto('>')) \ 1 &
move(1) &
count := (pos(0) & 1) | integer(tab(0))
}
then {
every 1 to count do
gener(goal)
return
}
else fail
end
# get right hand side of production
#
procedure getrhs(a)
local rhs
rhs := ""
every rhs ||:= listimage(!a) || "|"
return rhs[1:-1]
end
# look for request to write out grammar
#
procedure grammar(line)
local file, out, name
if line ? {
name := tab(find("->")) &
move(2) &
file := tab(0) &
out := if *file = 0 then &output else {
open(file,"w") | {
write(&errout,"*** cannot open ",file)
fail
}
}
}
then {
(*name = 0) | (name[1] == "<" & name[-1] == ">") | fail
pwrite(name,out)
if *file ~= 0 then close(out)
return
}
else fail
end
# produce image of list of grammar symbols
#
procedure listimage(a)
local s, x
s := ""
every x := !a do
s ||:= symimage(x)
return s
end
# look for new prompt symbol
#
procedure prompter(line)
if line[1] == "=" then {
prompt := line[2:0]
return
}
end
# write out grammar
#
procedure pwrite(name,ofile)
local nt, a
static builtin
initial builtin := ["lb","rb","vb","nl","","&lcase","&ucase","&digit"]
if *name = 0 then {
a := sort(defs,3)
while nt := get(a) do {
if nt == !builtin then {
get(a)
next
}
write(ofile,"<",nt,">::=",getrhs(get(a)))
}
}
else write(ofile,name,"::=",getrhs(\defs[name[2:-1]])) |
write("*** undefined nonterminal: ",name)
end
# look for file with input
#
procedure source(line)
local file
return line ? (="@" & push(ifile,in) & {
in := open(file := tab(0)) | {
write(&errout,"*** cannot open ",file)
fail
}
})
end
# produce string image of grammar symbol
#
procedure symimage(x)
return case type(x) of {
"string": x
"nonterm": "<" || x.name || ">"
"charset": "<'" || x.chars || "'>"
}
end
# process the symbols in an alternative
#
procedure syms(alt)
local slist
static nonbrack
initial nonbrack := ~'<'
slist := []
alt ? while put(slist,tab(many(nonbrack)) |
defnon(2(="<",tab(upto('>')),move(1))))
return slist
end
# stop, noting incorrect usage
#
procedure Usage()
stop("usage: [-t] [-l n] [-s n]")
end
procedure getopt(arg,optstring)
local x,i,c,otab,flist,o,p
/optstring := string(&lcase ++ &ucase)
otab := table()
flist := []
while x := get(arg) do
x ? {
if ="-" & not pos(0) then
while c := move(1) do
if i := find(c,optstring) + 1 then
otab[c] :=
if any(':+.',o := optstring[i]) then {
p := "" ~== tab(0) | get(arg) |
stop("No parameter following ",x)
case o of {
":": p
"+": integer(p) |
stop("-",c," needs numeric parameter")
".": real(p) |
stop("-",c," needs numeric parameter")
}
}
else 1
else stop("Unrecognized option: ",x)
else put(flist,x)
}
return [otab,flist]
end